package org.yscz.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.yscz.entity.Answer;
import org.yscz.entity.Report;
import org.yscz.entity.vo.*;
import org.yscz.mapper.*;
import org.yscz.service.RecordService;
import org.yscz.task.AbnormalEmotion;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @program: pe
 * @description: 测评记录表 Service接口实现类
 * @author: muqs
 * @create: 2021-08-24 11:07
 **/
@Service
@Slf4j
public class RecordServiceImpl implements RecordService {

    @Resource
    private RecordMapper recordMapper;

    @Resource
    private AnswerMapper answerMapper;

    @Resource
    private ReportMapper reportMapper;

    @Resource
    private TesteeMapper testeeMapper;

    @Resource
    private ScaleMapper scaleMapper;

    @Resource
    private QuestionMapper answerWrapper;

    @Value("${yscz.pictures.upload.httpLocation}")
    private String httpLocation;

    @Override
    public List<RecordVo> queryRecordDetail(Long testeeId, Integer status) {
        return recordMapper.queryRecordDetail(testeeId, status);
    }

    @Override
    public IPage<RecordVo> listPageByCondition(IPage<RecordVo> page, Map condition) {
        return recordMapper.listPageByCondition(page, condition);
    }

    @Override
    public void del(Long recordId) {
        // 删除测评记录信息
        recordMapper.deleteById(recordId);
        // 删除答题信息
        QueryWrapper answerWrapper = new QueryWrapper<>();
        answerWrapper.eq("record_id_fk", recordId);
        answerMapper.delete(answerWrapper);
        // 删除答题报告信息
        QueryWrapper reportWrapper = new QueryWrapper<>();
        reportWrapper.eq("record_id_fk", recordId);
        reportMapper.delete(reportWrapper);
    }

    @Override
    public JSONObject reportDetail(Long recordId) {
        JSONObject resultObj = new JSONObject();
        // 测评记录信息
        RecordVo recordVo = recordMapper.queryRecordDetailById(recordId);

        if (recordVo == null) {
            log.info("没有查询到测评记录数据,测评记录ID:{}", recordId);
            return null;
        }

        // 被测人信息
        TesteeVo testeeVo = testeeMapper.queryDetail(recordVo.getTesteeIdFk());
        resultObj.put("testeeInfo", testeeVo);

        // 测评报告信息
        QueryWrapper reportWrapper = new QueryWrapper();
        reportWrapper.eq("record_id_fk", recordId);
        Report report = reportMapper.selectOne(reportWrapper);
        resultObj.put("reportInfo", report);

        // 测评试题信息
        List<QuestionVo> questionVos = answerWrapper.queryByScaleId(recordVo.getScaleIdFk());
        Map<Long, QuestionVo> questionVosMap = new HashMap<>();
        if (questionVos != null) {
            for (QuestionVo questionVo : questionVos) {
                questionVosMap.put(questionVo.getQuestionId(), questionVo);
            }
        }

        // 测评试题回答信息
        QueryWrapper answerWrapper = new QueryWrapper();
        answerWrapper.eq("record_id_fk", recordId);
        answerWrapper.orderByAsc("answer_question_num");
        List<Answer> answers = answerMapper.selectList(answerWrapper);
        List<AnswerVo> answerVos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(answers)) {
            answers.stream().forEach(item -> {
                String imageUrl = item.getImageUrl();
                if(StringUtils.isNotEmpty(imageUrl)) {
                    List<AbnormalEmotion> abnormalEmotions = JSONArray.parseArray(imageUrl, AbnormalEmotion.class);
                    String collect = abnormalEmotions.stream().map(map -> httpLocation + map.getImageName() + "_" + map.getTimeStamp() + ".jpg").collect(Collectors.joining(","));
                    item.setImageUrl(collect);
                }
                AnswerVo answerVo = new AnswerVo();
                BeanUtils.copyProperties(item, answerVo);
                answerVo.setQuestionVo(questionVosMap.get(item.getQuestionIdFk()));
                answerVos.add(answerVo);
            });
        }
        resultObj.put("answerstInfo", answerVos);

        // 量表信息
        ScaleVo scaleVo = scaleMapper.queryDetailById(recordVo.getScaleIdFk());
        resultObj.put("scaleInfo", scaleVo);

        return resultObj;
    }
}
